package com.restaurant.ressys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.restaurant.ressys.entity.Comment;
import com.restaurant.ressys.entity.User;
import com.restaurant.ressys.service.CommentService;
import com.restaurant.ressys.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangsan
 * @since 2022-02-28
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Resource
    private CommentService commentService;
    @GetMapping("/getAll")
    public List<Comment> getAll(){
        return  commentService.list();
    }   
    @Resource
    private UserService us;
    @GetMapping("/selectByFid")
    public List<Comment> selectByFid(int fid){
        QueryWrapper<Comment> wrapper=new QueryWrapper<>();
        wrapper.eq("fid",fid);
        List<Comment> commentList = commentService.list(wrapper);
        return commentList.stream().peek((comment -> {
            User user = us.getById(comment.getUid());
            comment.setUser(user);
        })).collect(Collectors.toList());
    }
    @DeleteMapping("/del/{id}")
    public void del(@PathVariable Integer id){
        commentService.removeById(id);
    }
    @PostMapping("/add")
    public void add(@RequestBody Comment comment){
        comment.setCreateDate(LocalDateTime.now());
        commentService.save(comment);
    }
}

